/**
 * 权限实体
 * 作者：GodMainCode
 * 创建时间：2024-01-17
 * 修改时间：2024-01-17
 * 修改人：GodMainCode
 */

import { 
  Entity, 
  Column, 
  PrimaryGeneratedColumn, 
  CreateDateColumn, 
  UpdateDateColumn,
  ManyToMany,
  Tree,
  TreeChildren,
  TreeParent,
  JoinColumn
} from 'typeorm';
import { ApiProperty } from '@nestjs/swagger';
import { Role } from '../../role/entities/role.entity';

@Entity('permission')
@Tree("materialized-path")
export class Permission {
  @ApiProperty({ description: '权限ID' })
  @PrimaryGeneratedColumn({ comment: '权限ID' })
  id: number;

  @ApiProperty({ description: '权限名称' })
  @Column({ length: 50, comment: '权限名称' })
  name: string;

  @ApiProperty({ description: '权限标识' })
  @Column({ length: 50, unique: true, comment: '权限标识' })
  code: string;

  @ApiProperty({ description: '权限类型：1-目录 2-菜单 3-按钮' })
  @Column({ 
    type: 'smallint',
    default: 2,
    comment: '权限类型：1-目录 2-菜单 3-按钮'
  })
  type: number;

  @ApiProperty({ description: '图标' })
  @Column({ length: 50, nullable: true, comment: '图标' })
  icon: string;

  @ApiProperty({ description: '路由路径' })
  @Column({ length: 200, nullable: true, comment: '路由路径' })
  path: string;

  @ApiProperty({ description: '组件路径' })
  @Column({ length: 200, nullable: true, comment: '组件路径' })
  component: string;

  @ApiProperty({ description: '权限描述' })
  @Column({ length: 200, nullable: true, comment: '权限描述' })
  description: string;

  @ApiProperty({ description: '排序' })
  @Column({ type: 'int', default: 0, comment: '排序' })
  sort: number;

  @ApiProperty({ description: '父级ID' })
  @Column({ name: 'parent_id', type: 'int', nullable: true, comment: '父级ID' })
  parentId: number;

  @TreeChildren()
  children: Permission[];

  @TreeParent()
  @JoinColumn({ name: 'parent_id' })
  parent: Permission;

  @Column({ length: 255, nullable: true, comment: '物化路径' })
  mpath: string;

  @ApiProperty({ description: '状态：0-禁用 1-启用' })
  @Column({ 
    type: 'smallint',
    default: 1,
    comment: '状态：0-禁用 1-启用'
  })
  status: number;

  @ApiProperty({ description: '创建时间' })
  @CreateDateColumn({ 
    type: 'timestamp',
    nullable: false,
    default: () => 'CURRENT_TIMESTAMP',
    comment: '创建时间'
  })
  created_at: Date;

  @ApiProperty({ description: '更新时间' })
  @UpdateDateColumn({ 
    type: 'timestamp',
    nullable: false,
    default: () => 'CURRENT_TIMESTAMP',
    onUpdate: 'CURRENT_TIMESTAMP',
    comment: '更新时间'
  })
  updated_at: Date;

  @ApiProperty({ description: '是否删除：0-否 1-是' })
  @Column({ 
    type: 'smallint',
    default: 0,
    comment: '是否删除：0-否 1-是'
  })
  is_deleted: number;

  @ManyToMany(() => Role, role => role.permissions)
  roles: Role[];
} 